package cn.xiaoliu.pay.service.impl;

import cn.xiaoliu.basic.exception.BusinessException;
import cn.xiaoliu.basic.service.impl.BaseServiceImpl;
import cn.xiaoliu.order.service.IProductOrderService;
import cn.xiaoliu.pay.domain.AlipayInfo;
import cn.xiaoliu.pay.domain.PayBill;
import cn.xiaoliu.pay.mapper.AlipayInfoMapper;
import cn.xiaoliu.pay.mapper.PayBillMapper;
import cn.xiaoliu.pay.service.IPayBillService;
import cn.xiaoliu.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill bill) {
        if (bill == null || bill.getId() == null){
            throw new BusinessException("直接开喷");
        }
        PayBill payBill = payBillMapper.loadById(bill.getId());
        if (payBill == null){
            throw new BusinessException("直接开喷");
        }
        switch (payBill.getPayChannel().intValue()){
            case 1:{//支付宝支付
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(bill,info);
            }
            case 2:{//微信支付

            }
            case 3:{//银联支付

            }
            default:break;//其他支付
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
